﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

///<summary>
///汽泡道具
///</summary>
public enum PropNum
{
	Retarder,
	Lightning,
	Fire,
	Turrets,
	SpeedUp,
}

public class PoPPropComponent : MonoBehaviour
{
	//两跟随泡泡
	GameObject p1;
	GameObject p2;
	
	ImageSpriteChageComponet p1ChageComponent;
	ImageSpriteChageComponet p2ChageComponent;
	//爆开
	public  GameObject PopBoom;
	//当前汽泡的中的道具
	GameObject CurProp;
	PropNum propIcon;
	private void OnEnable()
	{
		//if (GameConfig.PropList.Count < 1) gameObject.SetActive(false);
		CurProp = transform.Find("PropIcon").gameObject;
		RandomItems();
	}

	// Start is called before the first frame update
	void Start()
    {
		p1 = transform.Find("p1").gameObject;
		p2 = transform.Find("p2").gameObject;
		p1ChageComponent = p1.GetComponent<ImageSpriteChageComponet>();
		p2ChageComponent = p2.GetComponent<ImageSpriteChageComponet>();
		
	}

    // Update is called once per frame
    void Update()
    {
		if (p1ChageComponent.aniFinish == true)
		{
			p1.SetActive(false);
		}
		if (p2ChageComponent.aniFinish == true)
		{
			p2.SetActive(false);
		}
		if (!p1.activeInHierarchy && !p2.activeInHierarchy)
		{
			StartCoroutine(Wait());
		}
		PopBoom.transform.position = transform.position;
		boom();
	}
	IEnumerator Wait()
	{
		yield return new  WaitForSeconds(0.5f);
		p1.SetActive(true);
		p2.SetActive(true);
	}
	public void boom()
	{
		if(Input.GetKeyDown(KeyCode.A))
		{
			gameObject.SetActive(false);
			PopBoom.GetComponent<Animation>().Play();
		}
	}
	//随机道具
	public void RandomItems()
	{
		RandomEnum();
		CurProp.GetComponent<Image>().sprite = Resources.Load<Sprite>("PropIcons/" + propIcon.ToString());
	}

	public void RandomEnum()
	{
        bool Is = true;
        if (GameConfig.PropList.Count < 1)
        {
            Debug.Log("目前没有拥有的道具，不能使用道具");
            gameObject.transform.parent.gameObject.SetActive(false);
            return; 
        }
		propIcon = (PropNum)Random.Range(0,GameConfig.PropList.Count);
		while (Is)
		{
			if (PlayerPrefs.HasKey(propIcon.ToString()))
			{
				Is = false;
				return;
			}
			else
			{
				propIcon++;
			}
		}
	}
	public PropNum GetPropIcon()
	{
		return propIcon;
	}
	
}
